/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpb.pp.lp.percistence;

import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Diogenes
 */
public class DAOJPA implements DAO {

    private EntityManagerFactory factory;
    private EntityManager em;

    public Boolean salvar(Object object) {
        try {
            open();
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
            return Boolean.TRUE;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return Boolean.FALSE;
        } finally {
            this.close();
        }
    }

    public Boolean remover(Object object) {
        try {
            open();
            em.getTransaction().begin();
            em.remove(object);
            em.getTransaction().commit();
            return Boolean.TRUE;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return Boolean.FALSE;
        } finally {
            this.close();
        }
    }

    public Boolean atualizar(Object object) {
        try {
            open();
            em.getTransaction().begin();
            em.merge(object);
            em.getTransaction().commit();
            return Boolean.TRUE;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return Boolean.FALSE;
        } finally {
            this.close();
        }
    }

    public <T> T buscar(Class<T> classe, Object id) {
        try {
            open();
            return em.find(classe, id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } 
    }

    public <T> List<T> listar(Class<T> classe) {
        try {
            open();
            Query consulta = em.createQuery("from " + classe.getSimpleName() + " t");
            List<T> t2 = consulta.getResultList();
            return t2;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close();
        }
        return null;
    }

    public <T> T executeSingleQuery(String query, Map<Integer, Object> param) {
        Object obj = null;
        try {
            open();
            Query consulta = em.createNamedQuery(query);
            for (Integer key : param.keySet()) {
                consulta.setParameter(key, param.get(key));
            }
            obj = consulta.getSingleResult();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close();
        }
        return (T) obj;
    }

    public <T> List<T> executeMultiplyQuery(String query, Map<Integer, Object> param) {
        List<T> lista = null;
        try {
            open();
            Query consulta = em.createNamedQuery(query);
            for (Integer key : param.keySet()) {
                consulta.setParameter(key, param.get(key));
            }
            lista = consulta.getResultList();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close();
        }
        return lista;

    }

    private void open() {
        if (this.factory == null || !this.factory.isOpen()) {
            this.factory = Persistence.createEntityManagerFactory("LojaDoPovoPU");
        }
        if (this.em == null || !this.em.isOpen()) {
            this.em = factory.createEntityManager();
        }
    }

    private void close() {
        if (this.em != null && this.em.isOpen()) {
            this.em.close();
        }
        if (this.factory != null && !this.factory.isOpen()) {
            this.factory.close();
        }
    }
}
